
import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.Circle;
import pluginUtils.Complexe; 
import pluginUtils.CircleInversion;

// Face centered cubic
public class PavageFCCBasicBallon extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	private double rayon=1; 
	private double rayonVerre=100; 
	
	private PrintStream output; 
	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	private String[] palette={"Yellow","Red","Blue","Yellow","White","NavyBlue","Turquoise","Orange"};
	private Color[] palette1={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE};
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	
	
	private static int number=0; 
	
	private void drawColoredCircle(Circle c,int i,double level){
		double xCenter=c.getCenter().getReal(); 
		double yCenter=c.getCenter().getIm(); 
		//if(c.getRadius()<0.001) return ; 
		
		int intX=(int)(xCenter*acoef+ccoef);
		int intY=(int)(yCenter*vcoef+wcoef);
		int intRx=(int)(c.getRadius()*acoef);
		int intRy=(int)(c.getRadius()*vcoef);
		
		primeIP.setColor(palette1[i]); 
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
		//if(c.getRadius()>5) return ; 
		
		//if((c.getCenter().squareNorm()<(rayonVerre-rayon)*(rayonVerre-rayon))&&(level>30)&&(level<200)){
		//if(c.getCenter().squareNorm()<20){
		double xx=c.getCenter().getReal(); 
		double zz=c.getCenter().getIm(); 
		if((Math.abs(xx)<10)&&(Math.abs(zz)<10)&&(level<14))
		{
		String s="sphere{\n<"+xCenter+","+level+","+yCenter+">,";
		s+=c.getRadius(); 
		
		s+=" texture{pigment{color "+palette[i]+"}} finish{reflection 0.6} }";
		
		//output.println(s);
		/*
		// issuing an octahedron
		String sp="object{icosahedron() texture{pigment{color "+palette[i]+"}} finish{reflection 0.8}"; 
		sp+="rotate<"+(1-2*generator.nextDouble())*Math.PI+","+(1-2*generator.nextDouble())*Math.PI+","+(1-2*generator.nextDouble())*Math.PI+"> "; 
		sp+="translate<"+ xCenter+","+level+","+yCenter+"> }";
		System.out.println(sp); 
		output.println(sp);
		*/
		// Planet earth
		String es="object{ballon finish{phong 0.3} rotate <"+generator.nextInt(360)+","+generator.nextInt(360)+","+generator.nextInt(360)+"> translate <"+ xCenter+","+level+","+yCenter+"> }"; 
		output.println(es); 
		System.out.println(es); 
		number++; 
		}
	}
	
	private void drawPoint(Complexe c){
		double xCenter=c.getReal(); 
		double yCenter=c.getIm(); 
		int intX=(int)(xCenter*acoef+ccoef);
		int intY=(int)(yCenter*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		double limit=40; 
	
		setBounds(-limit,limit,-limit,limit); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(5); 
		
		
		
		
		setProcessor("Verifying", primeIP);
		
		drawLine(x0,0,x1,0);
		drawLine(0,y0,0,y1);
		
		
		try{
			output=new PrintStream("/tmp/FCC.txt"); 
		double horizon=limit; 
		double cx=-horizon;
		double cy; 
		boolean tic=false; 
		int color=0; 
		double jump=6.0*Math.sqrt(2.0/3)*rayon; 
		while(cx<horizon){
			if(tic) cy=-horizon; else cy=-horizon+rayon;
			while(cy<horizon){
				for(int i=0;i<50;i++){
				Complexe centrum=Complexe.createComplexeFromRI(cx,cy);
				Complexe decaledCentrum1=Complexe.createComplexeFromRI(cx+Math.sqrt(1.0/3)*rayon,cy+rayon);
				Complexe decaledCentrum2=Complexe.createComplexeFromRI(cx-Math.sqrt(1.0/3)*rayon,cy+rayon);
				// Niveau A
				drawColoredCircle(new Circle(centrum,rayon),1,rayon+i*jump);
				// Niveau B
				drawColoredCircle(new Circle(decaledCentrum1,rayon),2,(1.0+2.0*Math.sqrt(2.0/3))*rayon+i*jump);
				// Niveau C
				drawColoredCircle(new Circle(decaledCentrum2,rayon),3,(1.0+4.0*Math.sqrt(2.0/3))*rayon+i*jump);
				}
				cy+=rayon*2; 
				}// cy
			cx=cx+Math.sqrt(3)*rayon; 
			tic=!tic; 
			color++; 
			System.out.println(cx); 
		}// cx
		}
		catch (Exception e) {
			System.out.println("plante"); 
		}
		
		
		show();
		System.out.println("done "+number); 
	}	
}
	